package com.eims.sls_app.ui.fragment_type;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.MD5;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.JxccsBetActivity;
import com.eims.sls_app.ui.adapter.JxccsGridAdapter;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.LotteryHelp;
import com.eims.sls_app.utils.NumberTools;
import com.eims.sls_app.view.MyGridView;
import com.eims.sls_app.view.MyToast;
import com.eims.sls_app.view.VibratorView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import bls.juejin.cn.R;

/**
 * 江西时时彩玩法Fragment
 */
@SuppressWarnings("RedundantIfStatement")
public class JxccsBaseFragment extends Fragment {
	public static boolean isvisible = true;
	private static final String ARG_PARAM1 = "ID";
	private static final String ARG_PARAM2 = "NAME";
	private static final String ARG_PARAM3 = "INDEX";
	private int TypeID;
	private String TypeName;
	private int index;
	private OnFragmentInteractionListener mListener;
	private Vibrator vibrator;
	private String opt, auth, info, time, imei; // 格式化后的参数
	private String[] dxds = { "大", "小", "单", "双" };
	private String[] dx = { "大", "小" };
	private int type = 0; // 0为普通，1为大小单双
	private boolean used = false;
	private boolean lablerused = false;
	private int count = 0;
	private List<String>[] data;
	private MyHandler myHandler;
	public static JxccsGridAdapter mAdapterOne;

	public static JxccsGridAdapter mAdapterTwo;

	public static JxccsGridAdapter mAdapterThree;

	public static JxccsGridAdapter mAdapterFour;

	public static JxccsGridAdapter mAdapterFive;
	String[] nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

	private ArrayList<String> list_yilou1 = new ArrayList<String>();
	private ArrayList<String> list_yilou2 = new ArrayList<String>();
	private ArrayList<String> list_yilou3 = new ArrayList<String>();
	private ArrayList<String> list_yilou4 = new ArrayList<String>();
	private ArrayList<String> list_yilou5 = new ArrayList<String>();

	// @BindView(R.id.number_sv_center_rlOne)
	RelativeLayout layout1;
	// @BindView(R.id.number_sv_center_rlTwo)
	RelativeLayout layout2;
	// @BindView(R.id.number_sv_center_rlThree)
	RelativeLayout layout3;
	// @BindView(R.id.number_sv_center_rlFour)
	RelativeLayout layout4;
	// @BindView(R.id.number_sv_center_rlFive)
	RelativeLayout layout5;

	// @BindView(R.id.tv_tip)
	TextView tv_tip;
	// @BindView(R.id.layout_shake)
	LinearLayout layout_shake;
	// @BindView(R.id.tv_show)
	TextView tv_show;
	// @BindView(R.id.tv_show2)
	TextView tv_show2;
	// @BindView(R.id.tv_show3)
	TextView tv_show3;
	// @BindView(R.id.tv_show4)
	TextView tv_show4;
	// @BindView(R.id.tv_show5)
	TextView tv_show5;

	// @BindView(R.id.number_sv_center_gv_showOne)
	MyGridView myGridView1;
	// @BindView(R.id.number_sv_center_gv_showTwo)
	MyGridView myGridView2;
	// @BindView(R.id.number_sv_center_gv_showThree)
	MyGridView myGridView3;
	// @BindView(R.id.number_sv_center_gv_showFour)
	MyGridView myGridView4;
	// @BindView(R.id.number_sv_center_gv_showFive)
	MyGridView myGridView5;

	/**
	 * @param TypeID
	 *            Parameter 1.
	 * @param TrpeName
	 *            Parameter 2.
	 */
	public static JxccsBaseFragment newInstance(int TypeID, String TrpeName,
                                                int index) {
		JxccsBaseFragment fragment = new JxccsBaseFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_PARAM1, TypeID);
		args.putString(ARG_PARAM2, TrpeName);
		args.putInt(ARG_PARAM3, index);
		fragment.setArguments(args);
		return fragment;
	}

	public JxccsBaseFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		myHandler = new MyHandler();
		if (getArguments() != null) {
			TypeID = getArguments().getInt(ARG_PARAM1);
			TypeName = getArguments().getString(ARG_PARAM2);
			index = getArguments().getInt(ARG_PARAM3);
		}
		MyAsynTask myAsyn = new MyAsynTask();
		myAsyn.execute();
	}

	/** 处理页面显示的 */
	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				setViewByID(TypeID);
				break;
			case -500:
				MyToast.getToast(getActivity(), "连接超时，请检查网络").show();
				break;
			}
			super.handleMessage(msg);
		}
	}

	/**
	 * 异步任务
	 */
	class MyAsynTask extends AsyncTask<Void, Integer, String> {
		/**
		 * 在后台执行的程序
		 */
		@Override
		protected String doInBackground(Void... params) {
			opt = "74";
			int playtype = 6113;
			if (TypeID > 61030) {
				playtype = 6103;
			} else {
				playtype = TypeID;
			}
			info = RspBodyBaseBean.getForgotNumber(playtype);
			auth = RspBodyBaseBean.changeLogin_Auth(
					RspBodyBaseBean.getCrc(time, imei,
							MD5.md5(App.MD5_key), info, "-1"), time, imei);
			String[] values = { opt, auth, info };
			Log.i("login", "遗漏值info---" + info);
			System.out.println("==============TypeID==" + TypeID);
			String result = HttpUtils.doPost(App.names, values,
					App.path);

			Log.i("login", "遗漏值result---" + result);
			if ("-500".equals(result))
				return "-500";
			try {
				// JSONObject item = new JSONObject(result);
				JSONObject isusesInfo = new JSONObject(result);
				JSONArray array = new JSONArray(isusesInfo.getString("miss"));
				JSONObject item = array.getJSONObject(0);

				if (list_yilou3.size() > 0 || list_yilou2.size() > 0
						|| list_yilou1.size() > 0 || list_yilou4.size() > 0
						|| list_yilou5.size() > 0) {
					list_yilou5.clear();
					list_yilou4.clear();
					list_yilou3.clear();
					list_yilou2.clear();
					list_yilou1.clear();
				}

				String str1 = "";
				String str2 = "";
				String str3 = "";
				String str4 = "";
				String str5 = "";

				if (TypeID == 6119 || TypeID == 6106 || TypeID == 6112
						|| TypeID == 6111 || TypeID == 6116 || TypeID == 6129
						|| TypeID == 6123 || TypeID == 6127 || TypeID == 6122
						|| TypeID == 6128) {
					str5 = item.optString("1");

				} else if (TypeID == 6130 || TypeID == 6131 || TypeID == 6132
						|| TypeID == 6124 || TypeID == 6126 || TypeID == 6120
						|| TypeID == 6121) {
					str4 = item.optString("1");
					str5 = item.optString("2");

				} else if (TypeID == 6104) {
					str4 = item.optString("1");
					str5 = item.optString("1");
				} else if (TypeID == 6125) {
					str3 = item.optString("1");
					str4 = item.optString("2");
					str5 = item.optString("3");

				} else if (TypeID == 6133) {
					str3 = item.optString("1");
					str4 = item.optString("2");
					str5 = item.optString("3");

				}
				// else if(TypeID==61034){ //前三直选
				// str2=item.optString("1");
				// str3=item.optString("2");
				// str4=item.optString("3");
				// str5=item.optString("4");
				//
				// }
				else if (TypeID == 6113 || TypeID == 6114 || TypeID == 6115
						|| TypeID == 6105 || TypeID == 61035 || TypeID == 61031
						|| TypeID == 61034 || TypeID == 61033
						|| TypeID == 61032) {

					if (TypeID == 61031) {
						str5 = item.optString("1");
					} else if (TypeID == 61032) {
						str4 = item.optString("1");
						str5 = item.optString("2");
					} else if (TypeID == 61033) {
						str3 = item.optString("1");
						str4 = item.optString("2");
						str5 = item.optString("3");
					} else if (TypeID == 61034) {
						str2 = item.optString("1");
						str3 = item.optString("2");
						str4 = item.optString("3");
						str5 = item.optString("4");
					} else {
						str1 = item.optString("1");
						str2 = item.optString("2");
						str3 = item.optString("3");
						str4 = item.optString("4");
						str5 = item.optString("5");
					}
				}

				if (null != str1 && !"".equals(str1))
					for (int i = 0; i < str1.split(",").length; i++) {
						list_yilou1.add(str1.split(",")[i]);
					}

				if (null != str2 && !"".equals(str2))
					for (int i = 0; i < str2.split(",").length; i++) {
						list_yilou2.add(str2.split(",")[i]);
					}

				if (null != str3 && !"".equals(str3))
					for (int i = 0; i < str3.split(",").length; i++) {
						list_yilou3.add(str3.split(",")[i]);
					}

				if (null != str4 && !"".equals(str4))
					for (int i = 0; i < str4.split(",").length; i++) {
						list_yilou4.add(str4.split(",")[i]);
					}

				if (null != str5 && !"".equals(str5))
					for (int i = 0; i < str5.split(",").length; i++) {
						list_yilou5.add(str5.split(",")[i]);
					}

				return "1";
			} catch (Exception ex) {
				return "-110";
			}
		}

		/**
		 * 最开始执行的
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO 这里要传递msg
			try {
				int error = Integer.parseInt(result);
				myHandler.sendEmptyMessage(error);
			} catch (Exception e) {
			}
			myHandler.sendEmptyMessage(Integer.parseInt(result));
			super.onPostExecute(result);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_jxccs_base, container,
				false);
		// ButterKnife.inject(this, view);
		initAll(view);
		return view;
	}

	private void initAll(View view) {
		initView(view);
		// 给gridview设置条目监听
		setItemListener();
		setViewByID(TypeID);
	}

	private void initView(View view) {
		layout1 = (RelativeLayout) view
				.findViewById(R.id.number_sv_center_rlOne);

		layout2 = (RelativeLayout) view
				.findViewById(R.id.number_sv_center_rlTwo);

		layout3 = (RelativeLayout) view
				.findViewById(R.id.number_sv_center_rlThree);

		layout4 = (RelativeLayout) view
				.findViewById(R.id.number_sv_center_rlFour);

		layout5 = (RelativeLayout) view
				.findViewById(R.id.number_sv_center_rlFive);

		tv_tip = (TextView) view.findViewById(R.id.tv_tip);

		layout_shake = (LinearLayout) view.findViewById(R.id.layout_shake);

		tv_show = (TextView) view.findViewById(R.id.tv_show);

		tv_show2 = (TextView) view.findViewById(R.id.tv_show2);

		tv_show3 = (TextView) view.findViewById(R.id.tv_show3);

		tv_show4 = (TextView) view.findViewById(R.id.tv_show4);

		tv_show5 = (TextView) view.findViewById(R.id.tv_show5);

		myGridView1 = (MyGridView) view
				.findViewById(R.id.number_sv_center_gv_showOne);

		myGridView2 = (MyGridView) view
				.findViewById(R.id.number_sv_center_gv_showTwo);

		myGridView3 = (MyGridView) view
				.findViewById(R.id.number_sv_center_gv_showThree);

		myGridView4 = (MyGridView) view
				.findViewById(R.id.number_sv_center_gv_showFour);

		myGridView5 = (MyGridView) view
				.findViewById(R.id.number_sv_center_gv_showFive);

	}

	/**
	 * 发起当前activity的回调
	 * 
	 * @param count
	 */
	public void onButtonPressed(int count) {
		if (mListener != null) {
			mListener.onFragmentInteraction(count);
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (OnFragmentInteractionListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnFragmentInteractionListener");
		}
	}

	/**
	 * 注册振动器
	 */
	@Override
	public void onResume() {
		super.onResume();
		vibrator = VibratorView.getVibrator(getActivity()
				.getApplicationContext());
	}

	/**
	 * 销毁振动器
	 */
	@Override
	public void onStop() {
		vibrator = null;
		super.onStop();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mListener = null;
	}

	public interface OnFragmentInteractionListener {
		public void onFragmentInteraction(int count);
	}

	/**
	 * 根据玩法id设置视图
	 * 
	 * @param ID
	 *            玩法id
	 */
	private void setViewByID(int ID) {
		switch (ID) {
		// 个位，非和值
		case 6119:
			layout_shake.setVisibility(View.GONE);
			setTipText("至少选择一个号码");
		case 6106:
			setTipText("至少选择二个号码");
		case 6112:
			setTipText("至少选择三个号码");
		case 6111:
			setTipText("至少选择二个号码");
		case 6116:
			setTipText("至少选择三个号码");
		case 6129:
			setTipText("至少选择四个号码");
		case 6123:
			setTipText("至少选择五个号码");
		case 6127:
		case 6128:
		case 6122:
			setTipText("至少选择一个号码");
			setHintText("选号");
		case 61031:
			setGridVisible(false, false, false, false, true);
			setAdapters(0);
			break;
		// 十个位
		case 6130:
			setTipText("二重至少选择一位，二单至少选择两位");
			setHintText("二单", "二重");
		case 6131:
			setHintText("二重", "二重");
		case 6132:
			setTipText("至少选择二个不同数字");
			setHintText("一单", "三重");
		case 6124:
			setTipText("二重至少选择一个号码，三单至少选择\n三个号码");
			setHintText("三单", "二重");
		case 6126:
			setTipText("三重至少选择一位，二单至少选择两位");
		case 6120:
			setTipText("至少选择二个不同数字");
			setHintText("二重", "三重");
		case 6121:
			setTipText("至少选择二个不同数字");
			setHintText("一单", "四重");
		case 61032:
			setGridVisible(false, false, false, true, true);
			setAdapters(0);
			break;
		// 百十个位
		case 6125:
			setTipText("至少选择三个不同数字");
			setHintText("一单", "二重", "二重");
		case 61033:
			setGridVisible(false, false, true, true, true);
			setAdapters(0);
			break;
		// 千百十个位
		case 61034:
			setGridVisible(false, true, true, true, true);
			setAdapters(0);
			break;
		// 万千百十个位
		case 6113:
			setTipText("请对个位、十位、百位、千位、万位\n其中任何一位进行投注");
		case 6114:
			setTipText("请对个位、十位、百位、千位、万位\n其中任何二位进行投注");
		case 6115:
			setTipText("请对个位、十位、百位、千位、万位\n其中任何三位进行投注");
		case 6105:
		case 61035:
			setGridVisible(true, true, true, true, true);
			setAdapters(0);
			break;
		// 和值
		case 6110:
		case 6117:
		case 6118:
			setTipText("至少选择一个和值");
			setHintText("选号");
			setGridVisible(false, false, false, false, true);
			setAdapters(2);
			break;
		// 大小单双
		case 6104:
			setTipText("请选择大小单双");
			setGridVisible(false, false, false, true, true);
			setAdapters(1);
			break;
		// 趣味二星
		case 6133:
			setTipText("每位至少选择一个值");
			setGridVisible(false, false, true, true, true);
			setAdapters(3);
			break;

		}
	}

	/**
	 * 隐藏或显示某些视图
	 */
	private void setGridVisible(boolean visible, boolean visible2,
			boolean visible3, boolean visible4, boolean visible5) {
		layout1.setVisibility(visible ? View.VISIBLE : View.GONE);
		layout2.setVisibility(visible2 ? View.VISIBLE : View.GONE);
		layout3.setVisibility(visible3 ? View.VISIBLE : View.GONE);
		layout4.setVisibility(visible4 ? View.VISIBLE : View.GONE);
		layout5.setVisibility(visible5 ? View.VISIBLE : View.GONE);
	}

	/**
	 * 普通玩法为mtype=0，大小单双为1，和值为2，趣味二星为3
	 */
	private void setAdapters(int mtype) {
		// 还原
		setGridItemListener(myGridView3, 3);
		switch (mtype) {
		case 0:

			if (layout1.getVisibility() == View.VISIBLE) {
				mAdapterOne = new JxccsGridAdapter(getActivity(), nums,
						list_yilou1, 1, isvisible);
				myGridView1.setAdapter(mAdapterOne);
			}
			if (layout2.getVisibility() == View.VISIBLE) {
				mAdapterTwo = new JxccsGridAdapter(getActivity(), nums,
						list_yilou2, 1, isvisible);
				myGridView2.setAdapter(mAdapterTwo);
			}
			if (layout3.getVisibility() == View.VISIBLE) {
				mAdapterThree = new JxccsGridAdapter(getActivity(), nums,
						list_yilou3, 1, isvisible);
				myGridView3.setAdapter(mAdapterThree);
			}
			if (layout4.getVisibility() == View.VISIBLE) {
				mAdapterFour = new JxccsGridAdapter(getActivity(), nums,
						list_yilou4, 1, isvisible);
				myGridView4.setAdapter(mAdapterFour);
			}
			if (layout5.getVisibility() == View.VISIBLE) {
				mAdapterFive = new JxccsGridAdapter(getActivity(), nums,
						list_yilou5, 1, isvisible);
				myGridView5.setAdapter(mAdapterFive);
			}
			break;
		case 1:// 大小单双
			type = 1;
			mAdapterFour = new JxccsGridAdapter(getActivity(), dxds, 1,
					isvisible, list_yilou4);
			myGridView4.setAdapter(mAdapterFour);
			mAdapterFive = new JxccsGridAdapter(getActivity(), dxds, 1,
					isvisible, list_yilou5);
			myGridView5.setAdapter(mAdapterFive);

			break;
		case 2:// 和值
			String HEZHI = "";
			if (TypeID == 6110) { // 二星组选和值
				HEZHI = "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18";
			} else if (TypeID == 6118 || TypeID == 6117) {// 6118三星组选和值
				// 6117三星直选和值
				HEZHI = "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27";
			}
			mAdapterFive = new JxccsGridAdapter(getActivity(),
					HEZHI.split(" "), list_yilou1, 1, isvisible);
			myGridView5.setAdapter(mAdapterFive);
			break;
		case 3:// 趣味二星
			type = 3;
			String[] num3 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
			mAdapterThree = new JxccsGridAdapter(getActivity(), dx, 1,
					isvisible, list_yilou3);
			myGridView3.setAdapter(mAdapterThree);
			// 改变百位的适配器
			setGridItemListener(myGridView3, 3);
			mAdapterFour = new JxccsGridAdapter(getActivity(), num3,
					list_yilou4, 1, isvisible);
			myGridView4.setAdapter(mAdapterFour);
			mAdapterFive = new JxccsGridAdapter(getActivity(), num3,
					list_yilou5, 1, isvisible);
			myGridView5.setAdapter(mAdapterFive);
			break;
		}
		updateAdapter(data);
	}

	// @OnClick(R.id.layout_shake)
	public void onShakeClick() {
		// Log.i("江西时时彩机选", "点击事件被触发");
		vibrator(300);
		clear();
		selectRandom(TypeID);
		setTotalCount();
	}

	// 点击事件
	public void onShakeClick(View view) {
		onShakeClick();
	}

	private void setItemListener() {
		setGridItemListener(myGridView1, 1);
		setGridItemListener(myGridView2, 2);
		setGridItemListener(myGridView3, 3);
		setGridItemListener(myGridView4, 4);
		setGridItemListener(myGridView5, 5);

	}

	private void setGridItemListener(MyGridView gridview, final int i) {
		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (i) {
				case 1:
					changeClickItem(mAdapterOne, position, type);
					break;
				case 2:
					changeClickItem(mAdapterTwo, position, type);
					break;
				case 3:
					if (type == 3) {
						changeClickItem(mAdapterThree, position, 4);
					} else {
						changeClickItem(mAdapterThree, position, type);
					}
					// changeClickItem(mAdapterThree, position, type);
					break;
				case 4:
					changeClickItem(mAdapterFour, position, type);
					break;
				case 5:
					changeClickItem(mAdapterFive, position, type);
					break;

				default:
					break;
				}
			}
		});

	}

	// @OnItemClick(R.id.number_sv_center_gv_showOne)
	// protected void onItemClick1(int position) {
	// changeClickItem(mAdapterOne, position, type);
	// }
	//
	// @OnItemClick(R.id.number_sv_center_gv_showTwo)
	// protected void onItemClick2(int position) {
	// changeClickItem(mAdapterTwo, position, type);
	// }
	//
	// @OnItemClick(R.id.number_sv_center_gv_showThree)
	// protected void onItemClick3(int position) {
	// changeClickItem(mAdapterThree, position, type == 3 ? 4 : type);
	// }
	//
	// @OnItemClick(R.id.number_sv_center_gv_showFour)
	// protected void onItemClick4(int position) {
	// changeClickItem(mAdapterFour, position, type);
	// }
	//
	// @OnItemClick(R.id.number_sv_center_gv_showFive)
	// protected void onItemClick5(int position) {
	// changeClickItem(mAdapterFive, position, type);
	// }

	/**
	 * 改变选中的背景
	 * 
	 * @param mAdapter
	 *            适配器
	 * @param position
	 *            位置
	 * @param type
	 *            类型
	 */
	private void changeClickItem(JxccsGridAdapter mAdapter, int position,
                                 int type) {
		if (mAdapter == null) {
			return;
		}
		vibrator(100);
		if (type == 0 || type == 3) {
			String str = position + "";
			if (mAdapter.contains(str)) {
				mAdapter.remove(str);
			} else {
				mAdapter.add(str);
			}
		} else if (type == 1) {
			// 大小单双
			String str = dxds[position];
			if (mAdapter.contains(str)) {
				mAdapter.remove(str);
			} else {
				mAdapter.clear();
				mAdapter.add(str);
			}
		} else if (type == 4) {
			// 趣味二星
			String str = dx[position];
			if (mAdapter.contains(str)) {
				mAdapter.remove(str);
			} else {
				mAdapter.add(str);
			}
		}
		mAdapter.notifyDataSetChanged();
		setTotalCount();
	}

	/**
	 * 更新总注数和金额
	 */
	private void setTotalCount() {

		if (type == 1) {
			// 大小单双
			count = mAdapterFour.getOneSet().size()
					* mAdapterFive.getOneSet().size();
		} else if (type == 3) {
			// 趣味二星
			count = mAdapterThree.getOneSet().size()
					* mAdapterFour.getOneSet().size()
					* mAdapterFive.getOneSet().size();
		} else {
			HashSet<String> set = new HashSet<String>();
			count = NumberTools.getJXSSC_count(mAdapterFive == null ? set
					: mAdapterFive.getOneSet(), mAdapterFour == null ? set
					: mAdapterFour.getOneSet(), mAdapterThree == null ? set
					: mAdapterThree.getOneSet(), mAdapterTwo == null ? set
					: mAdapterTwo.getOneSet(), mAdapterOne == null ? set
					: mAdapterOne.getOneSet(), index + 1);
			Log.i("jxssc", "===" + count);
		}
		onButtonPressed(count);
	}

	/**
	 * 设置位数标示
	 * 
	 * @param tip
	 *            标示
	 */
	private void setHintText(String... tip) {
		if (!used) {
			for (int i = 0; i < tip.length; i++) {
				if (i == 0) {
					tv_show5.setText(tip[0]);
				}
				if (i == 1) {
					tv_show4.setText(tip[1]);
				}
				if (i == 2) {
					tv_show3.setText(tip[2]);
				}
			}
			used = true;
		}
	}

	/**
	 * 设置玩法提醒
	 */
	private void setTipText(String text) {
		if (!lablerused) {
			lablerused = true;
			tv_tip.setText(text);
		}
	}

	/**
	 * 清空已选数据
	 */
	public void clear() {
		if (mAdapterOne != null) {
			mAdapterOne.clear();
			mAdapterOne.notifyDataSetChanged();
		}
		if (mAdapterTwo != null) {
			mAdapterTwo.clear();
			mAdapterTwo.notifyDataSetChanged();
		}
		if (mAdapterThree != null) {
			mAdapterThree.clear();
			mAdapterThree.notifyDataSetChanged();
		}
		if (mAdapterFour != null) {
			mAdapterFour.clear();
			mAdapterFour.notifyDataSetChanged();
		}
		if (mAdapterFive != null) {
			mAdapterFive.clear();
			mAdapterFive.notifyDataSetChanged();
		}
		count = 0;
	}

	/**
	 * 是否已选号码
	 * 
	 * @return
	 */
	public boolean hasSelected() {
		if (mAdapterOne != null && mAdapterOne.getOneSet().size() > 0) {
			return true;
		}
		if (mAdapterTwo != null && mAdapterTwo.getOneSet().size() > 0) {
			return true;
		}
		if (mAdapterThree != null && mAdapterThree.getOneSet().size() > 0) {
			return true;
		}
		if (mAdapterFour != null && mAdapterFour.getOneSet().size() > 0) {
			return true;
		}
		if (mAdapterFive != null && mAdapterFive.getOneSet().size() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 付款
	 */
	public void submit() {
		Intent intent = new Intent(getActivity(), JxccsBetActivity.class);
		Bundle bundle = new Bundle();
		if (type == 1) {
			// 大小单双
			if (mAdapterFour != null && mAdapterFive != null) {
				bundle.putString("four", mAdapterFour.getOneSet().toString()
						.replace("[", "").replace("]", "").replace(", ", ","));
				bundle.putString("five", mAdapterFive.getOneSet().toString()
						.replace("[", "").replace("]", "").replace(", ", ","));
			}
		} else if (type == 3) {
			// 趣味二星
			if (mAdapterThree != null) {
				bundle.putString("three", mAdapterThree.getOneSet().toString()
						.replace("[", "").replace("]", "").replace(", ", ","));
			}
			if (mAdapterFour != null) {
				bundle.putString("four",
						LotteryHelp.setToString(mAdapterFour.getOneSet()));
			}
			if (mAdapterFive != null) {
				bundle.putString("five",
						LotteryHelp.setToString(mAdapterFive.getOneSet()));
			}
		} else {
			if (mAdapterOne != null) {
				bundle.putString("one",
						LotteryHelp.setToString(mAdapterOne.getOneSet()));
			}
			if (mAdapterTwo != null) {
				bundle.putString("two",
						LotteryHelp.setToString(mAdapterTwo.getOneSet()));
			}
			if (mAdapterThree != null) {
				bundle.putString("three",
						LotteryHelp.setToString(mAdapterThree.getOneSet()));
			}
			if (mAdapterFour != null) {
				bundle.putString("four",
						LotteryHelp.setToString(mAdapterFour.getOneSet()));
			}
			if (mAdapterFive != null) {
				bundle.putString("five",
						LotteryHelp.setToString(mAdapterFive.getOneSet()));
			}
		}
		bundle.putInt("playType", TypeID);
		bundle.putInt("count", count);
		bundle.putString("playTypeName", TypeName);
		Log.e("Test-TypeName", TypeName);
		intent.putExtra("bundle", bundle);
		getActivity().startActivity(intent);
	}

	public void setAdapterData(List<String>... lists) {
		data = lists;
	}

	private void updateAdapter(List<String>... lists) {
		if (lists == null)
			return;
		for (int i = 0; i < lists.length; i++) {
			if (i == 0 && lists[i] != null) {
				if (mAdapterFive != null) {
					mAdapterFive.setOneSet(lists[0]);
					mAdapterFive.notifyDataSetChanged();
				}
			}
			if (i == 1 && lists[i] != null) {
				if (mAdapterFour != null) {
					mAdapterFour.setOneSet(lists[1]);
					mAdapterFour.notifyDataSetChanged();
				}
			}
			if (i == 2 && lists[i] != null) {
				if (mAdapterThree != null) {
					mAdapterThree.setOneSet(lists[2]);
					mAdapterThree.notifyDataSetChanged();
				}
			}
			if (i == 3 && lists[i] != null) {
				if (mAdapterTwo != null) {
					mAdapterTwo.setOneSet(lists[3]);
					mAdapterTwo.notifyDataSetChanged();
				}
			}
			if (i == 4 && lists[i] != null) {
				if (mAdapterOne != null) {
					mAdapterOne.setOneSet(lists[4]);
					mAdapterOne.notifyDataSetChanged();
				}
			}
		}
		setTotalCount();
	}

	/**
	 * 提供振动
	 */
	public void vibrator(int time) {
		if (null != vibrator)
			vibrator.vibrate(time);
	}

	/**
	 * 根据玩法随机选号
	 * 
	 * @param typeID
	 *            玩法id
	 */
	private void selectRandom(int typeID) {
		if (typeID == 6110) { // 二星和值
			setOneRandomSet(1, 18, mAdapterFive);
		} else if (typeID == 6118 || typeID == 6117) { // 三星和值
			setOneRandomSet(1, 27, mAdapterFive);
		} else if (typeID == 6104) { // 大小单双
			int index = (int) (Math.random() * 4);
			int index1 = (int) (Math.random() * 4);
			List<String> list = new ArrayList<String>();
			List<String> list1 = new ArrayList<String>();
			list.add(dxds[index]);
			list1.add(dxds[index1]);
			mAdapterFour.setOneSet(list);
			mAdapterFour.notifyDataSetChanged();
			mAdapterFive.setOneSet(list1);
			mAdapterFive.notifyDataSetChanged();
		} else if (typeID == 6133) { // 趣味二星
			int index = (int) (Math.random() * 2);
			List<String> list = new ArrayList<String>();
			list.add(dx[index]);
			mAdapterThree.setOneSet(list);
			mAdapterThree.notifyDataSetChanged();
			setOneRandomSet(1, 9, mAdapterFour, mAdapterFive);
		} else if (typeID == 61031 || typeID == 6127 || typeID == 6128
				|| typeID == 6122) {
			setOneRandomSet(1, 9, mAdapterFive);
		} else if (typeID == 6106 || typeID == 6111) {
			setOneRandomSet(2, 9, mAdapterFive);
		} else if (typeID == 6112 || typeID == 6116) {
			setOneRandomSet(3, 9, mAdapterFive);
		} else if (typeID == 6129) {
			setOneRandomSet(4, 9, mAdapterFive);
		} else if (typeID == 6123) {
			setOneRandomSet(5, 9, mAdapterFive);
		} else if (typeID == 61032) {
			setOneRandomSet(1, 9, mAdapterFour, mAdapterFive);
		} else if (typeID == 6130 || typeID == 6126) {
			List<String> temp = NumberTools.getRandomNum(1, 9);
			List<String> temp1 = NumberTools.getRandomNum(temp, 2, 9);
			setOneRandomSet(
					new JxccsGridAdapter[] { mAdapterFour, mAdapterFive },
					temp, temp1);
		} else if (typeID == 6131 || typeID == 6132 || typeID == 6120
				|| typeID == 6121) {
			List<String> temp = NumberTools.getRandomNum(1, 9);
			List<String> temp1 = NumberTools.getRandomNum(temp, 1, 9);
			setOneRandomSet(
					new JxccsGridAdapter[] { mAdapterFour, mAdapterFive },
					temp, temp1);
		} else if (typeID == 6124) {
			List<String> temp = NumberTools.getRandomNum(1, 9);
			List<String> temp1 = NumberTools.getRandomNum(temp, 3, 9);
			setOneRandomSet(
					new JxccsGridAdapter[] { mAdapterFour, mAdapterFive },
					temp, temp1);
		} else if (typeID == 61033) {
			setOneRandomSet(1, 9, mAdapterThree, mAdapterFour, mAdapterFive);
		} else if (typeID == 6125) {
			List<String> temp = NumberTools.getRandomNum(1, 9);
			List<String> temp1 = NumberTools.getRandomNum(temp, 1, 9);
			List<String> temp2 = new ArrayList<String>();
			for (String s : temp) {
				temp2.add(s);
			}
			for (String s1 : temp1) {
				temp2.add(s1);
			}
			List<String> temp3 = NumberTools.getRandomNum(temp2, 1, 9);
			setOneRandomSet(new JxccsGridAdapter[] { mAdapterThree,
					mAdapterFour, mAdapterFive }, temp, temp1, temp3);
		} else if (typeID == 61034) {
			setOneRandomSet(1, 9, mAdapterTwo, mAdapterThree, mAdapterFour,
					mAdapterFive);
		} else if (typeID == 61035 || typeID == 6105) {
			setOneRandomSet(1, 9, mAdapterOne, mAdapterTwo, mAdapterThree,
					mAdapterFour, mAdapterFive);
		} else if (typeID == 6113) {
			JxccsGridAdapter[] adapters = new JxccsGridAdapter[] { mAdapterOne,
					mAdapterTwo, mAdapterThree, mAdapterFour, mAdapterFive };
			setOneRandomSet(1, 9, adapters[NumberTools.getRandomNumInt(1, 4)
					.get(0)]);
		} else if (typeID == 6114) {
			JxccsGridAdapter[] adapters = new JxccsGridAdapter[] { mAdapterOne,
					mAdapterTwo, mAdapterThree, mAdapterFour, mAdapterFive };
			List<Integer> in = NumberTools.getRandomNumInt(2, 4);
			setOneRandomSet(1, 9, adapters[in.get(0)], adapters[in.get(1)]);
		} else if (typeID == 6115) {
			JxccsGridAdapter[] adapters = new JxccsGridAdapter[] { mAdapterOne,
					mAdapterTwo, mAdapterThree, mAdapterFour, mAdapterFive };
			List<Integer> in = NumberTools.getRandomNumInt(3, 4);
			setOneRandomSet(1, 9, adapters[in.get(0)], adapters[in.get(1)],
					adapters[in.get(2)]);
		}
	}

	private void setOneRandomSet(int count, int max,
			JxccsGridAdapter... mAdapters) {
		for (JxccsGridAdapter adapter : mAdapters) {
			adapter.setOneSet(NumberTools.getRandomNum(count, max));
			adapter.notifyDataSetChanged();
		}
	}

	private void setOneRandomSet(JxccsGridAdapter[] mAdapters,
			List<String>... list) {
		for (int i = 0; i < mAdapters.length; i++) {
			mAdapters[i].setOneSet(list[i]);
			mAdapters[i].notifyDataSetChanged();
		}
	}
}
